package com.xjc.market.frame.tag;

import java.io.IOException;
import java.util.Calendar;

import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

/**
 * 隐藏部分内容
 * @param content	被隐藏内容
 * @param percent	隐藏百分比
 * @param show		true表示不用隐藏	
 * @return
 */
public class Mytag4hideContent extends BodyTagSupport{
	public static String suffix = "<div class='login-to-view'>更多内容只对特定用户可见</div>";
	public static Integer HIDE_CONTENT_PERCENT = 10;
	private static final long serialVersionUID = 1L;
	private String content;
	private Integer percent;
	private Boolean show = false;

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Integer getPercent() {
		return percent;
	}

	public void setPercent(Integer percent) {
		this.percent = percent;
	}

	public Boolean getShow() {
		return show;
	}

	public void setShow(Boolean show) {
		this.show = show;
	}

	public int doStartTag(){
		JspWriter out= pageContext.getOut();
		
		String result = "";
		if(content==null || content.equals("")){}
		else if(content.length()<500 || show!=null && show==true || percent!=null && percent>=100) result = content;
		else{
			if(percent==null) percent = HIDE_CONTENT_PERCENT;
			
			int len = content.length() * percent / 100;
			
			Document doc = Jsoup.parse(content.substring(0, len)+" ……<br/>");
			result = doc.body().html()+suffix;
		}
		try {
			out.print(result);
		} catch (IOException e) {}
		
		return EVAL_BODY_INCLUDE;
	}
	
	public int doEndTag(){
        return  EVAL_BODY_INCLUDE;
	}
}
